package com.ruoyi.strategy.service.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;


import com.ruoyi.dto.StrategyWithTypeDto;
import com.ruoyi.strategwithtype.domain.StrategyType;
import com.ruoyi.strategwithtype.mapper.StrategyTypeMapper;

import com.ruoyi.type.domain.Type;
import com.ruoyi.vo.StrategyWithTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.strategy.mapper.StrategyMapper;
import com.ruoyi.strategy.domain.Strategy;
import com.ruoyi.strategy.service.IStrategyService;

/**
 * strategyService业务层处理
 *
 * @author sywc
 * @date 2025-01-09
 */
@Service
public class StrategyServiceImpl implements IStrategyService
{
    @Autowired
    private StrategyMapper strategyMapper;


    @Autowired
    private StrategyTypeMapper strategyTypeMapper;

    /**
     * 查询strategy
     *
     * @param id strategy主键
     * @return strategy
     */
    @Override
    public Strategy selectStrategyById(Integer id)
    {
        return strategyMapper.selectStrategyById(id);
    }

    /**
     * 查询strategy列表
     *
     * @param strategy strategy
     * @return strategy
     */
    @Override
    public List<StrategyWithTypeVo> selectStrategyList(Strategy strategy) {
        //1.动态的查询出strategy
        List<Strategy> strategies = strategyMapper.selectStrategyList(strategy);
        //2.将List<Strategy>转化为List<StrategyWithTypeVo>
        return selectStrategyCommon(strategies);
    }
    //将List<Strategy>  变为  List<StrategyWithTypeVo>
    public List<StrategyWithTypeVo> selectStrategyCommon(List<Strategy> strategiesList){
        List<StrategyWithTypeVo> list = new LinkedList<>();
        for (Strategy strategy : strategiesList) {
            StrategyWithTypeVo swtVo = new StrategyWithTypeVo();
            swtVo.setStrategy(strategy);
            List<Type> types = selectTypeByStrategyId(strategy.getId());
            swtVo.setTypeList(types);
            list.add(swtVo);
        }
        return  list;
    }
    //查询出策略拥有的type
    public List<Type> selectTypeByStrategyId(Integer StrategyId) {
        //查询中间表得到，typeIds
        List<Integer> ids = strategyMapper.selectTypeId(StrategyId, 1);
        List<Type> typeList =  new ArrayList<>();
        for (Integer id : ids) {
            //得到类型·并且添加
            typeList.add(strategyMapper.selectTypeByStrategyId(id, 1)) ;
        }
        return typeList;
    }
    /**
     * 新增strategy
     *
     * @param strategy strategy
     * @return 结果
     */
    @Override
    public int insertStrategy(Strategy strategy)
    {
        //判断当前要插入的strategy的策略名称是否重复
        Strategy s = strategyMapper.selectStrategyByName(strategy.getName());
        if(!Objects.isNull(s) && s.getIsDeleted()==0){//这个策略之前添加过，只需要更新is_deleted 和对应的信息
            s.setIsDeleted(1);
            s.setDescription(strategy.getDescription());
            strategyMapper.updateStrategy(s);
        } else if (!Objects.isNull(s) && s.getIsDeleted()==1) {//这个策略当前在数据库中已经存在
            return -1;
        }
        return strategyMapper.insertStrategy(strategy);
    }

    /**
     * 修改strategy
     *
     * @param StrategyWithTypeDto swtDto
     * @return 结果
     */
    @Override
    public int updateStrategy(Strategy strategy) {
        //注意：需要更新则表示一定存在，is_deleted == 1
//        Strategy strategy = swtDto.getStrategy();
        //更新策略表
        return strategyMapper.updateStrategy(strategy);
       /* //更新中间表
        for (int i = 0; i < swtDto.getTypeIdList().size(); i++) {
            Integer strategyId = strategy.getId();
            Integer typeId = swtDto.getTypeIdList().get(i);
            //更新中间表，当前更新的type在数据库中已经存在
            StrategyType exist = strategyTypeMapper.selectStrategyTypeExist(strategyId, typeId);
            if (!Objects.isNull(exist) && exist.getIsDeleted() == 1) {
                return -1;
            }
            //更新中间表，当前更新的type的is_deleted == 0
            if (!Objects.isNull(exist) && exist.getIsDeleted() == 0) {
                return strategyTypeMapper.updateStrategyType(exist);
            }
            //更新中间表，当前更新的type在数据库中从来没有插入过，则直接插入
            return strategyTypeMapper.insertStrategyWithType(strategyId, typeId);
        }
        return 1;*/
    }

    /**
     * 批量删除strategy
     *
     * @param ids 需要删除的strategy主键
     * @return 结果
     */
    @Override
    public int deleteStrategyByIds(Integer[] ids)
    {
        return strategyMapper.deleteStrategyByIds(ids);
    }

    /**
     * 删除strategy信息
     *
     * @param id strategy主键
     * @return 结果
     */
    @Override
    public int deleteStrategyById(Integer id)
    {
        return strategyMapper.deleteStrategyById(id);
    }
}
